package com.example.exam.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.exam.pojo.User;
import com.example.exam.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhozixiong
 * @date 2023/03/15
 */
public class UserListener extends AnalysisEventListener<User> {

    private List<User> list = new ArrayList<>();

    private UserService userService;

    public UserListener(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void invoke(User user, AnalysisContext analysisContext) {
        list.add(user);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        for(User user : list) {
            userService.insert(user);
        }
    }
}
